package ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ui.CreateEventPanel.EventPanelListener;
import entity.EntityConstants;
import entity.Event;

/**
 * A Panel that contains options to generate and modify 
 * recurrence rules.
 * 
 * @author Sean Young
 * @version 1.0
 *
 */
public class RRulePanel extends JPanel implements EntityConstants, ItemListener{
	private JComboBox frequencyBox, dailyCount;
	private JPanel frequencyPanel, cardPanel, buttonPanel;
	private JButton done, cancel;
	private DailyPanel dailyPanel;
	private WeeklyPanel weeklyPanel;
	private MonthlyPanel monthlyPanel;
	private YearlyPanel yearlyPanel;
	private CreateEventPanel parent;
	private RRulePanelListener listener;
	private JDialog container;
	
	/**
	 * Constructor for the RRulePanel. Requires a reference to the 
	 * CreateEventPanel to add the RRULE to the current event 
	 * as well as the container frame.
	 * @param parent The CreateEventPanel
	 * @param container the container frame
	 */
	public RRulePanel(CreateEventPanel parent, JDialog container) {
		super();
		setLayout(new BorderLayout());
		this.parent = parent;
		this.container = container;
		listener = new RRulePanelListener();
		
		frequencyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		String[] frequencyOptions = {"Daily", "Weekly", "Monthly", "Yearly"};
		frequencyBox = new JComboBox<String>(frequencyOptions);
		frequencyBox.addItemListener(this);
		frequencyPanel.add(new JLabel("Repeat "));
		frequencyPanel.add(frequencyBox);
		add(frequencyPanel, BorderLayout.NORTH);
		
		dailyPanel = new DailyPanel(this.parent);
		weeklyPanel = new WeeklyPanel(this.parent);
		monthlyPanel = new MonthlyPanel(this.parent);
		yearlyPanel = new YearlyPanel(this.parent);
		
		cardPanel = new JPanel(new CardLayout());
		cardPanel.add(dailyPanel, "Daily");
		cardPanel.add(weeklyPanel, "Weekly");
		cardPanel.add(monthlyPanel, "Monthly");
		cardPanel.add(yearlyPanel, "Yearly");
		add(cardPanel, BorderLayout.CENTER);
		
		buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		done = new JButton("Done");
		done.setActionCommand("done");
		done.addActionListener(listener);
		buttonPanel.add(done);
		
		cancel = new JButton("Cancel");
		cancel.setActionCommand("cancel");
		cancel.addActionListener(listener);
		buttonPanel.add(cancel);
		
		add(buttonPanel, BorderLayout.SOUTH);
		
		this.setPreferredSize(new Dimension(435, 275));
	}
	
	/**
	 * Changes the option panel based upon the frequency
	 * selected in the JComboBox
	 */
	public void itemStateChanged(ItemEvent evt) {
		CardLayout cl = (CardLayout) (cardPanel.getLayout());
		cl.show(cardPanel, (String) evt.getItem());
	}
	/**
	 * Creates and displays the RRULE panel as a pop-up 
	 * window.
	 * @param parent The CreateEventPanel
	 * @return The RRulePanel
	 */
	public static RRulePanel showRRuleWindow(CreateEventPanel parent){
		JDialog frame = new JDialog(new JFrame(),"Repeat");
		//frame.setSize(new Dimension(435, 275));
		//frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		RRulePanel window = new RRulePanel(parent, frame);
		ImageIcon icon = new ImageIcon("images/Calendar-3-256x256.png");
		frame.setIconImage(icon.getImage());
		frame.add(window);
		frame.pack();
		frame.setLocationRelativeTo(parent);
		frame.setAlwaysOnTop(true);
		//frame.setResizable(false);
		frame.setVisible(true);
		return window;
	}
/**
 * Action listener that performs actions based upon the 
 * button pressed in the RRulePanel.
 *
 */
public class RRulePanelListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			switch(e.getActionCommand()){
				case "done" :  switch (frequencyBox.getSelectedIndex()) {
							      case 0: parent.addRRule(dailyPanel.buildRRule());
							      		  break;
							      case 1: parent.addRRule(weeklyPanel.buildRRule());
							      		  break;
							      case 2: parent.addRRule(monthlyPanel.buildRRule());
							      		  break;
							      case 3: parent.addRRule(yearlyPanel.buildRRule());
							      		  break;
							   }
							   container.dispose();
							   break;
				case "cancel": container.dispose();
							   break;
										  
			}
		}	
	}
	
	
}
